package com.czk.producer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Arrays;
import java.util.Properties;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/5/24 17:46
 */
public class InterceptorProducer {
    public static void main(String[] args) {
        // 创建kafka的配置信息
        Properties properties = new Properties();
        // 连接kafka信息
        properties.put("bootstrap.servers", "192.168.25.133:9092");
        // 自动确认
        properties.put("acks", "all");
        // 重试次数
        properties.put("retries", 3);
        // 批次大小
        properties.put("batch.size", 1638);
        // 等待时间
        properties.put("linger.ms", 3);
        // RecordAccumulator缓冲区大小
        properties.put("buffer.memory", 33554432);
        // 指定序列化key-value类型
        properties.put("key.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        // 添加拦截器
        properties.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, Arrays.asList("com.czk.interceptors.TimerInterceptor", "com.czk.interceptors.CounterInterceptor"));

        // 创建kafka生产者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);
        // 发送数据
        for (int i = 0; i < 10; i++) {
            kafkaProducer.send(new ProducerRecord<>("first", "czk--" + i));

        }
        // 清理内存资源
        kafkaProducer.close();
    }
}
